/*************************************************************
   Copyright(C) 2022 by WuIslet
   All rights reserved.
   
   LocalConfigManager.cs
   UnityFramework
   
   Created by WuIslet on 2022-04-06.
   
*************************************************************/

using System.Collections;
using System.Collections.Generic;
using System.IO;

public enum LocalConfigserializationType
{
    PlayerPref,
#if UNITY_EDITOR
    EditorPref,
#endif
    Json,
    ProtoBuf,
    CSV,
    ScriptableObject,
}

public class LocalConfigManager : leapMotor.Singleton<LocalConfigManager>
{
    private LocalConfigserializationType serializationType = LocalConfigserializationType.PlayerPref;
    private string serializationExtraParam = "";
    public Dictionary<string, string> kvMap = new Dictionary<string, string>();
    public void SetLocalConfigBaseSetting(LocalConfigserializationType type, string extra)
    {
        serializationType = type;
        serializationExtraParam = extra;
        kvMap.Clear();
    }

    public void DeserializationConfig()
    {
        if (serializationType == LocalConfigserializationType.Json)
        {
            if (string.IsNullOrEmpty(serializationExtraParam))
                return;

            using (FileStream fs = new FileStream(serializationExtraParam, FileMode.OpenOrCreate, FileAccess.Read))
            {
                StreamReader sr = new StreamReader(fs);
                var json = sr.ReadToEnd();
                var data = LitJson.JsonMapper.ToObject(json);
                kvMap.Clear();
                var dict = data as IDictionary;
                var keys = dict.Keys;
                foreach (var key in keys) //todo 性能
                {
                    var sKey = (string)key;
                    kvMap.Add(sKey, data[sKey] == null ? "" : data[sKey].ToString());
                }
                sr.Close();
            }
        }
        else if (serializationType == LocalConfigserializationType.ProtoBuf)
        {
            if (string.IsNullOrEmpty(serializationExtraParam))
                return;

        }
    }

    public void SerializationConfig()
    {
        if (serializationType == LocalConfigserializationType.Json)
        {
            if (string.IsNullOrEmpty(serializationExtraParam))
                return;

            using (FileStream fs = new FileStream(serializationExtraParam, FileMode.Create, FileAccess.Write))
            {
                var json = LitJson.JsonMapper.ToJson(kvMap);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(json);
                sw.Close();
            }
        }
        else if (serializationType == LocalConfigserializationType.ProtoBuf)
        {
            if (string.IsNullOrEmpty(serializationExtraParam))
                return;
        }
    }

    public string GetRawData(string key, string defaultReturn = "", bool isForceRefresh = false) //todo string转化性能
    {
        if (serializationType == LocalConfigserializationType.Json)
        {
            if (kvMap.Count == 0 || isForceRefresh) //todo 性能
            {
                DeserializationConfig();
            }

            var result = defaultReturn;
            kvMap.TryGetValue(key, out result);
            return result;
        }
        return defaultReturn;
    }

    public void SetRawData(string key, string val)
    {
        if (serializationType == LocalConfigserializationType.Json)
        {
            if (kvMap.ContainsKey(key))
            {
                kvMap[key] = val;
            }
            else
            {
                kvMap.Add(key, val);
            }
            SerializationConfig(); //todo 性能
        }
    }

    //public override void Destroy()
    //{
    //    serializationType = LocalConfigserializationType.PlayerPref;
    //    serializationExtraParam = "";
    //    kvMap.Clear();
    //}
}